export default {
  namespaced:true,
  state:{
    attr:{
      width:375,
      height:667,
    },
    index:0,
    newList:{}, //当前移动参数
    content:[],
    setContent:[], // 替换模式的content
    ghostClass:"" //设置拖动元素的占位符类名
  },
  mutations:{
    getSessionContent(state){
      let content = sessionStorage.getItem("content")
      if(content){
        content = JSON.parse(content)
        state.content = content.data
        state.index = content.index
      }
    },
    saveNewList(state,value){
      state.newList = value
    },
    saveContent(state,res){
      let content = JSON.parse(JSON.stringify(state.content))
      let oldIndex = res.oldIndex
      let newIndex = res.newIndex
      if(oldIndex != undefined){ // 替换模式
        content = state.setContent
      }else{ // 新增模式
        if(newIndex <= state.index){
          for(let i in content){
            if(i >= newIndex){
              let o = parseInt(i) + 1
              content[o] = state.content[i]
            }
          }
          res.element.index = state.index
          content[newIndex] = res.element
        }else{
          res.element.index = state.index
          content.push(res.element)
        }
        state.index++
      }
      state.content = content
    },
    setGhostClass(state,value){
      state.ghostClass = value
    },
    saveSetContent(state,value){
      state.setContent = value
    },
    removeContent(state,index){
      let idx = state.content.findIndex(e=>e.index == index)
      state.content.splice(idx,1)
      let s = {
        data:state.content,
        index:state.index
      }
      sessionStorage.setItem("content",JSON.stringify(s))
    }
  },
  actions:{
    changeContent(context,res){
      let o = context.state.content.find(e=>e.index == res.id)
      // console.log(res.attr)
      o.attr = res.attr
      let s = {
        data:context.state.content,
        index:context.state.index
      }
      sessionStorage.setItem("content",JSON.stringify(s))
    },
  }
}